package main

import (
	"io/ioutil"
	"log"
	"mysocks5/socks5"
	"mysocks5/util"
	"net"
	"net/http"
)

func init() {
	//初始化配置
	util.InitCfg()

}
func main() {
	//test.Test()
	//log.Println("Ignore frag")
	ExampleServer()
	//ExampleClient_tcp()
}

func ExampleServer() {
	log.Println("socks5 service start")
	//var ip = "192.168.31.184"
	//var port = "1080"
	var ip = "192.168.16.4"
	var port = "1081"
	//var s1 = "127.0.0.1:1080"
	//var s2 = "127.0.0.1"

	//var s1 = "172.16.0.11:1081"
	//var s2 = "172.16.0.11"
	//var s1="127.0.0.1:1080"
	//s, err := socks5.NewClassicServer("127.0.0.1:1080", "127.0.0.1", "", "", 0, 60)
	s, err := socks5.NewClassicServer(ip, port, "", "", 0, 60)
	if err != nil {
		log.Println(err)
		return
	}
	// You can pass in custom Handler
	err = s.ListenAndServe(nil)
	if err != nil {
		log.Println(err)
	}
	// #Output:
}
func ExampleClient_tcp() {
	log.Println("socks5 client start")
	//go ExampleServer()
	//var s1 = "49.232.79.155:1080"
	//var s1 = "180.76.243.116:1081"
	var s1 = "120.48.126.27:1081"

	//var s1 = "192.168.31.184:1080"
	c, err := socks5.NewClient(s1, "", "", 0, 60)
	if err != nil {
		log.Println(err)
		return
	}
	client := &http.Client{
		Transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				return c.Dial(network, addr)
			},
		},
	}
	res, err := client.Get("https://ifconfig.co")
	if err != nil {
		log.Println(err)
		return
	}
	defer res.Body.Close()
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("tcp", string(b))
	// Output:
}
